package yagwl.client.widget;

import yagwl.client.resource.Resources;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.SplitLayoutPanel;
import com.google.gwt.user.client.ui.Widget;

public class ScrollingSplitPanel extends Composite {

  private final Grid rootG = new Grid(4, 1);
  private final SplitLayoutPanel splitPanel = new SplitLayoutPanel();
  private int splitPanelHeight = 200;

  public ScrollingSplitPanel(int height) {
    rootG.setWidth("100%");
    rootG.setWidget(0, 0, new Image(Resources.I.thumbProgress()));
    rootG.getCellFormatter().setHorizontalAlignment(0, 0,
        HasHorizontalAlignment.ALIGN_CENTER);
    splitPanelHeight = height < 20 ? 20 : height;
    splitPanel.setHeight(splitPanelHeight + "px");
    rootG.setWidget(2, 0, splitPanel);
    rootG.setWidget(3, 0, getHeightAdjustor());
    rootG.getCellFormatter().setHorizontalAlignment(3, 0,
        HasHorizontalAlignment.ALIGN_RIGHT);
    rootG.getRowFormatter().setVisible(1, false);
    rootG.getRowFormatter().setVisible(2, false);
    rootG.getRowFormatter().setVisible(3, false);
    initWidget(rootG);
  }

  public void setActionWidget(Widget widget) {
    rootG.setWidget(1, 0, widget);
  }

  public void addWest(Widget widget, double size) {
    splitPanel.addWest(new ScrollPanel(widget), size);
  }

  public void addNorth(Widget widget, double size) {
    splitPanel.addWest(new ScrollPanel(widget), size);
  }

  public void addSouth(Widget widget, double size) {
    splitPanel.addSouth(new ScrollPanel(widget), size);
  }

  public void addEast(Widget widget, double size) {
    splitPanel.addEast(new ScrollPanel(widget), size);
  }

  public void add(Widget widget) {
    splitPanel.add(new ScrollPanel(widget));
  }

  public void show() {
    rootG.getRowFormatter().setVisible(0, false);
    rootG.getRowFormatter().setVisible(1, true);
    rootG.getRowFormatter().setVisible(2, true);
    rootG.getRowFormatter().setVisible(3, true);
  }

  /**
   * Sets the height of the split panel
   */
  public void setSplitPanelHeight(int height) {
    if (height <= 10)
      return;
    this.splitPanelHeight = height;
    splitPanel.setHeight(height + "px");
  }

  private Widget getHeightAdjustor() {
    HorizontalPanel hPanel = new HorizontalPanel();
    Image down = new Image(Resources.I.getArrowDown());
    Image up = new Image(Resources.I.getArrowUp());
    down.setTitle("Make taller");
    up.setTitle("Make shorter");
    hPanel.add(down);
    hPanel.add(new Spacer(10, 1));
    hPanel.add(up);
    hPanel.add(new Spacer(20, 1));
    down.addStyleName("ya-tree-action");
    up.addStyleName("ya-tree-action");
    down.addClickHandler(new ClickHandler() {
      @Override
      public void onClick(ClickEvent event) {
        splitPanelHeight = splitPanelHeight + 50;
        splitPanel.setHeight(splitPanelHeight + "px");
      }
    });
    up.addClickHandler(new ClickHandler() {
      @Override
      public void onClick(ClickEvent event) {
        if (splitPanelHeight > 60)
          splitPanelHeight = splitPanelHeight - 50;
        splitPanel.setHeight(splitPanelHeight + "px");
      }
    });
    return hPanel;
  }

}
